package com.rubulls.report;


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;


import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.Image;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.Barcode128;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class Prototype {
	
	//int[] table_pos = {14,54,76,98,112,152,174,196};
	int[] table_pos = {32,78,102,124};
	
	public static final String DEST = "results/tables/simple_table.pdf";
	public static final String DEST2 = "results/tables/small_table.pdf";
	
	public static BaseFont bfChinese ;
	
	private static String srcDir = "E:/workspaces/wsHuixiangdou/reports/20130612/321102002/";
	private static String inputFile = "RCDE0017_321102002_01.txt";//"RGLBD004_0_321102002_04.txt";
	
	private Charset gbCs = Charset.forName("GB2312");
	
	private TableInfo ti = new TableInfo( new int[] {32,78,102,124});
	
	 

	public static void main(String[] args) throws IOException,
    DocumentException {
		
		bfChinese = BaseFont.createFont("/SIMSUNB.TTF", BaseFont.IDENTITY_H,  
	            BaseFont.NOT_EMBEDDED);   
		
		
		File file = new File(DEST);
		file.getParentFile().mkdirs();

		
		Prototype pt = new Prototype();
		//pt.createPdf2(DEST2);
		
		String fileName = srcDir + inputFile;
		
		pt.processFile(fileName,null);
	
	}
	
    public void createPdf2(String dest) throws IOException, DocumentException {

    	
        Rectangle small = new Rectangle(290,100);
        Font smallfont = new Font(bfChinese, 10);
        Document document = new Document(small, 5, 5, 5, 5);
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        PdfPTable table = new PdfPTable(2);
        table.setTotalWidth(new float[]{ 160, 120 });
        table.setLockedWidth(true);
        PdfContentByte cb = writer.getDirectContent();
        // first row
        PdfPCell cell = new PdfPCell(new Phrase("许多数字"));
        cell.setFixedHeight(30);
        //cell.setBorder(Rectangle.NO_BORDER);
        cell.setColspan(2);
        table.addCell(cell);
        // second row
        cell = new PdfPCell(new Phrase("科目代码", smallfont));
        cell.setFixedHeight(30);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        //cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        Barcode128 code128 = new Barcode128();
        code128.setCode("14785236987541");
        code128.setCodeType(Barcode128.CODE128);
        Image code128Image = code128.createImageWithBarcode(cb, null, null);
        cell = new PdfPCell(code128Image, true);
        //cell.setBorder(Rectangle.NO_BORDER);
        cell.setFixedHeight(30);
        table.addCell(cell);
        // third row
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("表格许多ʾ", smallfont));
        //cell.setBorder(Rectangle.NO_BORDER);
        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        table.addCell(cell);
        document.add(table);
        document.close();
    }
    
    public void processFile(String source,String dest) throws IOException, DocumentException {
    
    	
    	//final  String[] fields = new String[table_pos.length];
    	boolean tableOpen = false;
    	try{
    		Stream<String> stream = Files.lines(Paths.get(source),gbCs);
    		
    		List<String> lines = stream.collect(Collectors.toList());
    		String[] fields;
    		
    		for(String s:lines){
    			
    			if(s.contains("┏"))
    				tableOpen = true;
    			if(s.contains("┛"))
    				tableOpen = false;

    			if(s.contains("━━"))
    				System.out.println("Skip");
    			else{
    				
    				if(tableOpen){
    					fields = Parser.ParseLine2(s);
    				
    					int n = fields.length;
    				}
    				
    				System.out.println(s);
    			}
    		};
    	}
    	catch(Exception e){
    		System.out.println(e.toString());
    	}
    	

    	
    	
    
    }
    
    

}
